<template>
  <div class="dashboard-container">
    <!-- 时钟卡片 -->
    <el-card class="clock-card">
      <template #header>
        <div class="card-header">
          <div class="header-title">
            <i class="el-icon-time"></i>
            此时此刻
          </div>
          <el-tag size="small" :type="getTimeTagType()" class="header-tag">
            {{ getTimeGreeting() }}
          </el-tag>
        </div>
      </template>
      <div class="clock">
        <div class="time-wrapper">
          <div class="time">{{ currentTime }}</div>
          <div class="time-shadow"></div>
        </div>
        <div class="date">{{ currentDate }}</div>
        <div class="weather-info">
          <i class="el-icon-sunny"></i>
          <span>今日天气晴朗</span>
          <span class="temperature">23°C</span>
        </div>
        <div class="quote-box">
          <div class="quote-icon">💡</div>
          <span class="quote">{{ randomQuote }}</span>
        </div>
      </div>
    </el-card>

    <!-- 日历卡片 -->
    <el-card class="calendar-card">
      <template #header>
        <div class="card-header">
          <div class="header-title">
            <i class="el-icon-date"></i>
            日历
          </div>
          <div class="calendar-controls">
            <el-tag size="small" type="success">
              {{ currentDay.toLocaleDateString('zh-CN', { month: 'long' }) }}
            </el-tag>
          </div>
        </div>
      </template>
      <el-calendar v-model="currentDay">
        <template #dateCell="{ data }">
          <div class="calendar-cell"
               :class="{
                 'is-today': isToday(data.day),
                 'is-weekend': isWeekend(data.day)
               }">
            <span>{{ data.day.split('-').slice(2).join('') }}</span>
            <div class="cell-indicator" v-if="isHoliday(data.day)">🎉</div>
            <div class="cell-indicator" v-else-if="isWeekend(data.day)">🌟</div>
          </div>
        </template>
      </el-calendar>
    </el-card>

    <!-- 新年倒计时卡片 -->
    <el-card class="countdown-card">
      <template #header>
        <div class="card-header">
          <div class="header-title">
            <i class="el-icon-timer"></i>
            倒计时
          </div>
          <el-tag size="small" type="warning" class="header-tag">
            {{ getCountdownStatus() }}
          </el-tag>
        </div>
      </template>
      <div class="countdown">
        <h3>距离{{ nextYear }}年还有</h3>
        <div class="countdown-grid">
          <div class="countdown-item" v-for="(value, key) in countdown" :key="key">
            <div class="number-wrapper">
              <span class="number">{{ value }}</span>
              <div class="number-shadow"></div>
            </div>
            <span class="unit">{{ getUnit(key) }}</span>
          </div>
        </div>
        <div class="progress-wrapper">
          <div class="progress-text">年度进度</div>
          <el-progress
            :percentage="getYearProgress()"
            :color="progressColors"
            :stroke-width="15"
            :show-text="false"
          />
          <div class="progress-value">{{ getYearProgress() }}%</div>
        </div>
        <div class="countdown-tip">
          <i class="el-icon-bell"></i>
          {{ getCountdownTip() }}
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'

const currentTime = ref('')
const currentDate = ref('')
const currentDay = ref(new Date())
const nextYear = new Date().getFullYear() + 1
const randomQuote = ref('')

const quotes = [
  "今天也是元气满满的一天！",
  "摸鱼的一天也要过得开心哦～",
  "休息是为了更好的摸鱼！",
  "今天你学习了吗？没有？那就对了！",
  "距离下班还有无数秒...",
]

const countdown = ref({
  years: 0,
  months: 0,
  days: 0,
  hours: 0,
  minutes: 0,
  seconds: 0
})

// // 获取当前心情
// const getCurrentMood = () => {
//   const hour = new Date().getHours()
//   if (hour < 6) return '该睡觉了！'
//   if (hour < 9) return '早安呀！'
//   if (hour < 12) return '上午好！'
//   if (hour < 14) return '午饭时间！'
//   if (hour < 18) return '打起精神！'
//   if (hour < 22) return '晚上好！'
//   return '早点休息哦！'
// }

// 获取倒计时心情
// const getCountdownMood = () => {
//   if (countdown.value.days > 300) return '来日方长~'
//   if (countdown.value.days > 100) return '慢慢来~'
//   if (countdown.value.days > 30) return '即将到来！'
//   if (countdown.value.days > 10) return '准备跨年！'
//   return '就要到啦！'
// }

// 判断是否是节假日
const isHoliday = (day) => {
  const holidays = ['2024-12-25', '2025-01-01', '2025-02-19'] // 可以添加更多节日
  return holidays.includes(day)
}

// 获取日期提示
// const getDayTip = () => {
  // const day = new Date().getDay()
  // const tips = [
    // '周日快乐',
    // '周一加油',
    // '周二继续',
    // '周三加把劲',
    // '周四坚持',
    // 'TGIF!',
    // '周六愉快'
  // ]
  // return tips[day]
// }

// 获取倒计时提示
const getCountdownTip = () => {
  const { days } = countdown.value
  if (days > 300) return '新的一年还早着呢~'
  if (days > 100) return '时间过得真快啊！'
  if (days > 30) return '准备开始写新年计划了吗？'
  if (days > 10) return '该准备跨年啦！'
  return '新年快乐！🎉'
}

// 更新时间
const updateTime = () => {
  const now = new Date()
  currentTime.value = now.toLocaleTimeString('zh-CN', {
    hour12: false,
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit'
  })
  currentDate.value = now.toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: 'long',
    day: 'numeric',
    weekday: 'long'
  })
}

// 更新倒计时
const updateCountdown = () => {
  const now = new Date()
  const newYear = new Date(nextYear, 0, 1)
  const diff = newYear - now

  // 计算年月日时分秒
  const years = newYear.getFullYear() - now.getFullYear()
  const months = newYear.getMonth() - now.getMonth()
  const days = Math.floor(diff / (1000 * 60 * 60 * 24))
  const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))
  const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60))
  const seconds = Math.floor((diff % (1000 * 60)) / 1000)

  countdown.value = {
    years,
    months: months < 0 ? months + 12 : months,
    days,
    hours,
    minutes,
    seconds
  }
}

// 更新随机名言
const updateQuote = () => {
  const index = Math.floor(Math.random() * quotes.length)
  randomQuote.value = quotes[index]
}

let timeInterval
let countdownInterval
let quoteInterval

onMounted(() => {
  updateTime()
  updateCountdown()
  updateQuote()
  timeInterval = setInterval(updateTime, 1000)
  countdownInterval = setInterval(updateCountdown, 1000)
  quoteInterval = setInterval(updateQuote, 30000) // 每30秒更换一次名言
})

onUnmounted(() => {
  clearInterval(timeInterval)
  clearInterval(countdownInterval)
  clearInterval(quoteInterval)
})

// 新增方法
const getTimeGreeting = () => {
  const hour = new Date().getHours()
  if (hour < 6) return '夜深了 🌙'
  if (hour < 9) return '早安 ☀️'
  if (hour < 12) return '上午好 🌤'
  if (hour < 14) return '午餐时间 🍱'
  if (hour < 18) return '下午好 ⛅'
  if (hour < 22) return '晚上好 🌆'
  return '该休息了 🌙'
}

const getTimeTagType = () => {
  const hour = new Date().getHours()
  if (hour < 6 || hour >= 22) return 'info'
  if (hour < 12) return 'success'
  if (hour < 18) return 'warning'
  return 'primary'
}

const isWeekend = (day) => {
  const date = new Date(day)
  return date.getDay() === 0 || date.getDay() === 6
}

const getYearProgress = () => {
  const now = new Date()
  const start = new Date(now.getFullYear(), 0, 1)
  const end = new Date(now.getFullYear() + 1, 0, 1)
  return Math.round(((now - start) / (end - start)) * 100)
}

const progressColors = [
  { color: '#f56c6c', percentage: 20 },
  { color: '#e6a23c', percentage: 40 },
  { color: '#5cb87a', percentage: 60 },
  { color: '#1989fa', percentage: 80 },
  { color: '#6f7ad3', percentage: 100 }
]

const getCountdownStatus = () => {
  const { days } = countdown.value
  if (days > 300) return '来日方长 🌱'
  if (days > 200) return '稳步前进 🚶'
  if (days > 100) return '时间飞逝 ⏰'
  if (days > 50) return '即将到来 🎯'
  return '倒计时 🎊'
}

const isToday = (day) => {
  const today = new Date().toISOString().split('T')[0]
  return day === today
}

const getUnit = (key) => {
  const units = {
    years: '年',
    months: '月',
    days: '天',
    hours: '时',
    minutes: '分',
    seconds: '秒'
  }
  return units[key]
}
</script>

<style scoped>
/* 导入动画库 */
@import 'animate.css';

.dashboard-container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
  padding: 20px;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  height: 100%;
  box-sizing: border-box;
}

/* 卡片通用样式 */
.el-card {
  height: calc(100vh - 100px);
  border-radius: 24px !important;
  overflow: hidden;
  border: none !important;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  backdrop-filter: blur(10px);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.05) !important;
}

.el-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1) !important;
}

/* 时钟卡片样式 */
.clock-card {
  background: linear-gradient(145deg, #ffffff, #e6f7ff);
}

.time-wrapper {
  padding: 30px 0;
}

.time {
  font-size: calc(3vw + 1rem);
  line-height: 1.2;
  color: #409EFF;
  font-weight: bold;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.time-shadow {
  position: absolute;
  bottom: -5px;
  left: 50%;
  transform: translateX(-50%);
  width: 80%;
  height: 10px;
  background: rgba(64, 158, 255, 0.1);
  filter: blur(8px);
  border-radius: 50%;
}

.date {
  font-size: 1.4em;
  color: #333;
  margin: 20px 0;
  font-weight: 600;
}

.weather-info {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin: 15px 0;
  padding: 10px;
  background: rgba(64, 158, 255, 0.1);
  border-radius: 12px;
  color: #409EFF;
}

.temperature {
  font-weight: bold;
  color: #f56c6c;
}

.quote-box {
  background: rgba(64, 158, 255, 0.05);
  padding: 15px;
  border-radius: 12px;
  margin: 20px;
}

.quote {
  font-size: 1.1em;
  color: #666;
  font-style: italic;
  margin: 0 10px;
}

/* 日历卡片样式 */
.calendar-card {
  background: linear-gradient(145deg, #ffffff, #f0f9eb);
}

:deep(.el-calendar) {
  height: 100%;
  background: transparent;
}

:deep(.el-calendar__header) {
  padding: 12px;
  border-radius: 16px;
  margin: 0 10px;
  color: #333;
  font-weight: 600;
}

:deep(.el-calendar-day) {
  height: 40px;
  border-radius: 12px;
  transition: all 0.3s ease;
  color: #333;
  font-weight: 500;
}

.calendar-cell {
  position: relative;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.calendar-cell.is-today {
  background: rgba(64, 158, 255, 0.1);
  font-weight: bold;
}

.calendar-cell:hover {
  background: rgba(64, 158, 255, 0.05);
  transform: scale(1.1);
}

.calendar-cell.is-weekend {
  color: #f56c6c;
  background: rgba(245, 108, 108, 0.1);
}

.cell-indicator {
  position: absolute;
  top: 2px;
  right: 2px;
  font-size: 12px;
}

/* 倒计时卡片样式 */
.countdown-card {
  background: linear-gradient(145deg, #ffffff, #fff8f0);
}

.countdown-time {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 15px;
  padding: 20px;
}

.number {
  font-size: calc(1.5vw + 0.5rem);
  padding: 15px;
  border-radius: 16px;
  min-width: 60px;
  color: #409EFF;
  font-weight: bold;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.number-wrapper {
  background: rgba(255, 255, 255, 0.6);
  border-radius: 16px;
  padding: 5px;
  box-shadow:
    0 4px 15px rgba(64, 158, 255, 0.1),
    inset 0 -2px 5px rgba(0, 0, 0, 0.03);
}

.number-shadow {
  position: absolute;
  bottom: -5px;
  left: 50%;
  transform: translateX(-50%);
  width: 80%;
  height: 8px;
  background: rgba(64, 158, 255, 0.1);
  filter: blur(6px);
  border-radius: 50%;
}

.unit {
  font-size: 1.1em;
  color: #333;
  margin-top: 8px;
  font-weight: 600;
}

.countdown-tip {
  background: rgba(64, 158, 255, 0.1);
  padding: 12px;
  border-radius: 8px;
  color: #666;
  font-size: 1.1em;
  margin: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

/* 卡片头部样式 */
:deep(.el-card__header) {
  padding: 0;
  border-bottom: none;
  background: transparent;
}

.card-header {
  position: relative;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.5), rgba(255, 255, 255, 0.3));
  backdrop-filter: blur(10px);
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.header-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  position: relative;
  padding: 0 20px;
}

.header-title::before,
.header-title::after {
  content: '';
  position: absolute;
  top: 50%;
  width: 15px;
  height: 2px;
  background: linear-gradient(90deg, #409EFF, transparent);
}

.header-title::before {
  right: 100%;
}

.header-title::after {
  left: 100%;
  transform: rotate(180deg);
}

.header-tag {
  position: absolute;
  right: 16px;
  padding: 0 12px;
  height: 24px;
  line-height: 24px;
  border-radius: 12px;
  font-size: 12px;
  color: #333;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(4px);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
}

/* 卡片内容区域调整 */
:deep(.el-card__body) {
  padding-top: 20px;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .dashboard-container {
    grid-template-columns: repeat(3, 1fr);
    gap: 15px;
    padding: 15px;
  }

  .time {
    font-size: calc(2vw + 1rem);
  }

  .number {
    font-size: calc(1.2vw + 0.5rem);
    padding: 10px;
    min-width: 50px;
  }

  .header-title {
    font-size: 16px;
  }

  .header-tag {
    font-size: 11px;
    padding: 0 8px;
  }

  .countdown-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}
</style>
